<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="format-detection" content="telephone=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="icon" href="/blog/blog/images/icons/favicon-16x16.png?v=2.6.2" type="image/png" sizes="16x16"><link rel="icon" href="/blog/blog/images/icons/favicon-32x32.png?v=2.6.2" type="image/png" sizes="32x32"><meta name="description" content=".back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;} .back-top&gt;a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height:">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring学习笔记(所有代码FROM SIKI学院)">
<meta property="og:url" content="https://bruce-jj.gitee.io/blog/2021/03/16/Spring/index.html">
<meta property="og:site_name" content="斯人若彩虹 遇上方知有">
<meta property="og:description" content=".back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;} .back-top&gt;a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height:">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-03-16T06:49:57.000Z">
<meta property="article:modified_time" content="2021-03-17T07:21:32.000Z">
<meta property="article:author" content="何水浪">
<meta property="article:tag" content="SSM学习笔记">
<meta name="twitter:card" content="summary"><title>Spring学习笔记(所有代码FROM SIKI学院) | 斯人若彩虹 遇上方知有</title><link ref="canonical" href="https://bruce-jj.gitee.io/blog/2021/03/16/Spring/"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.12.1/css/all.min.css" type="text/css"><link rel="stylesheet" href="/blog/css/index.css?v=2.6.2"><script>var Stun = window.Stun || {};
var CONFIG = {
  root: '/blog/',
  algolia: undefined,
  assistSearch: undefined,
  fontIcon: {"prompt":{"success":"fas fa-check-circle","info":"fas fa-arrow-circle-right","warning":"fas fa-exclamation-circle","error":"fas fa-times-circle"},"copyBtn":"fas fa-copy"},
  sidebar: {"offsetTop":"20px","tocMaxDepth":6},
  header: {"enable":true,"showOnPost":true,"scrollDownIcon":false},
  postWidget: {"endText":true},
  nightMode: {"enable":true},
  back2top: {"enable":true},
  codeblock: {"style":"default","highlight":"light","wordWrap":false},
  reward: false,
  fancybox: false,
  zoomImage: {"gapAside":"20px"},
  galleryWaterfall: undefined,
  lazyload: false,
  pjax: undefined,
  externalLink: {"icon":{"enable":true,"name":"fas fa-external-link-alt"}},
  shortcuts: undefined,
  prompt: {"copyButton":"复制","copySuccess":"复制成功","copyError":"复制失败"},
  sourcePath: {"js":"js","css":"css","images":"images"},
};

window.CONFIG = CONFIG;</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/blog/atom.xml" title="斯人若彩虹 遇上方知有" type="application/atom+xml">
</head><body><div class="container" id="container"><header class="header" id="header"><div class="header-inner"><nav class="header-nav header-nav--fixed"><div class="header-nav-inner"><div class="header-nav-menubtn"><i class="fas fa-bars"></i></div><div class="header-nav-menu"><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/"><span class="header-nav-menu-item__icon"><i class="fas fa-home"></i></span><span class="header-nav-menu-item__text">首页</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/archives/"><span class="header-nav-menu-item__icon"><i class="fas fa-folder-open"></i></span><span class="header-nav-menu-item__text">归档</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/categories/"><span class="header-nav-menu-item__icon"><i class="fas fa-layer-group"></i></span><span class="header-nav-menu-item__text">分类</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/tags/"><span class="header-nav-menu-item__icon"><i class="fas fa-tags"></i></span><span class="header-nav-menu-item__text">标签</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/friends/"><span class="header-nav-menu-item__icon"><i class="fas fa-link"></i></span><span class="header-nav-menu-item__text">友链</span></a></div></div><div class="header-nav-search"><span class="header-nav-search__icon"><i class="fas fa-search"></i></span><span class="header-nav-search__text">搜索</span></div><div class="header-nav-mode"><div class="mode"><div class="mode-track"><span class="mode-track-moon"></span><span class="mode-track-sun"></span></div><div class="mode-thumb"></div></div></div></div></nav><div class="header-banner"><div class="header-banner-info"><div class="header-banner-info__title">斯人若彩虹 遇上方知有</div><div class="header-banner-info__subtitle"></div></div></div></div></header><main class="main" id="main"><div class="main-inner"><div class="content-wrap" id="content-wrap"><div class="content" id="content"><!-- Just used to judge whether it is an article page--><div id="is-post"></div><div class="post"><header class="post-header"><h1 class="post-title">Spring学习笔记(所有代码FROM SIKI学院)</h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-16</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-03-17</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">5.5k</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">45分</span></span></div></header><div class="post-body"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->


        <h1 id="SSM框架系列之二-大管家Spring"   >
          <a href="#SSM框架系列之二-大管家Spring" class="heading-link"><i class="fas fa-link"></i></a><a href="#SSM框架系列之二-大管家Spring" class="headerlink" title="SSM框架系列之二 大管家Spring"></a>SSM框架系列之二 大管家Spring</h1>
      
        <h2 id="Spring是什么？"   >
          <a href="#Spring是什么？" class="heading-link"><i class="fas fa-link"></i></a><a href="#Spring是什么？" class="headerlink" title="Spring是什么？"></a>Spring是什么？</h2>
      <p>Spring是于2003 年兴起的一个轻量级的Java 开源框架，它由Rod Johnson创建。传统J2EE应用的开发效率低，Spring作为开源的中间件，提供J2EE应用的各层的解决方案，Spring贯穿了表现层、业务层及持久层，而不是仅仅专注于某一层的方案。可以说Spring是企业应用开发的“一站式（full-stack）”选择。然而，Spring并不想取代那些已有的框架，而是与它们无缝地整合。<br>简单来说，Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架。</p>
<p>为什么要使用Spring？</p>
<ol>
<li><p>方便解耦，简化开发<br>通过Spring提供的IoC容器，我们可以将对象之间的依赖关系交由Spring进行控制，避免硬编码所造成的过度程序耦合。有了Spring，用户不必再为单实例模式类、属性文件解析等这些很底层的需求编写代码，可以更专注于上层的应用。</p>
</li>
<li><p>AOP编程的支持<br>通过Spring提供的AOP功能，方便进行面向切面的编程，许多不容易用传统OOP实现的功能可以通过AOP轻松应付。</p>
</li>
<li><p>声明式事务的支持<br>在Spring中，我们可以从单调烦闷的事务管理代码中解脱出来，通过声明式方式灵活地进行事务的管理，提高开发效率和质量。</p>
</li>
<li><p>方便程序的测试<br>可以用非容器依赖的编程方式进行几乎所有的测试工作，在Spring里，测试不再是昂贵的操作，而是随手可做的事情。例如：Spring对Junit4支持，可以通过注解方便的测试Spring程序。</p>
</li>
<li><p>方便集成各种优秀框架<br>Spring不排斥各种优秀的开源框架，相反，Spring可以降低各种框架的使用难度，Spring提供了对各种优秀框架（如Struts、Hibernate、MyBatis 、Hessian、Quartz）等的直接支持。</p>
</li>
<li><p>降低Java EE API的使用难度<br>Spring对很多难用的Java EE API（如JDBC，JavaMail，远程调用等）提供了一个薄薄的封装层，通过Spring的简易封装，这些Java EE API的使用难度大为降低。</p>
</li>
</ol>

        <h2 id="我能从这门课中学到什么？"   >
          <a href="#我能从这门课中学到什么？" class="heading-link"><i class="fas fa-link"></i></a><a href="#我能从这门课中学到什么？" class="headerlink" title="我能从这门课中学到什么？"></a>我能从这门课中学到什么？</h2>
      <ol>
<li>了解Spring框架、IOC、DI的概念；</li>
<li>掌握Spring框架搭建、配置与使用；</li>
<li>掌握Spring容器创建与对象管理；</li>
<li>掌握Spring xml配置式开发与注解开发；</li>
<li>掌握Spring AOP相关知识；</li>
<li>掌握SpringJDBC相关知识；</li>
<li>将Spring集成项目中进行测试；</li>
</ol>

        <h1 id="开始学习Spring框架"   >
          <a href="#开始学习Spring框架" class="heading-link"><i class="fas fa-link"></i></a><a href="#开始学习Spring框架" class="headerlink" title="开始学习Spring框架"></a>开始学习Spring框架</h1>
      
        <h2 id="Part-1-Spring框架介绍与入门案例："   >
          <a href="#Part-1-Spring框架介绍与入门案例：" class="heading-link"><i class="fas fa-link"></i></a><a href="#Part-1-Spring框架介绍与入门案例：" class="headerlink" title="Part.1 Spring框架介绍与入门案例："></a>Part.1 Spring框架介绍与入门案例：</h2>
      <p>1、搭建Spring HelloWorld项目<br>    a)Spring下载：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="http://repo.spring.io/release/org/springframework/spring/" >http://repo.spring.io/release/org/springframework/spring/</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>    b)导包：（以下5个包称为基本包，根据你使用的版本可能还需导入log4j包，咱们学习的版本暂时无需导入）<br>        i.核心包：bean、context、core、expression；<br>        ii.日志包：apache.commons.logging<br>    c)引入约束：bean约束和引入主配置文件头；<br>    d)测试：将自定义对象由自己创建交由Spring管理；</p>
<p>2、SpringIDE - sts插件安装；<br>    a)下载地址：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="http://spring.io/tools/sts/all/" >http://spring.io/tools/sts/all/</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>    b)安装springIDE；<br>        Spring-IoC | DI概念介绍：（要实现IOC依赖DI的支持）<br>        IoC（Inversion of Control）：反转控制：将我们自己创建对象的工作交给Spring容器帮我们完成；<br>        DI（Dependency Injection）：依赖注入：将值通过配置的方式为变量初始化/赋值(注入)；<br>    c)注入方式：set方法注入、构造注入、字段注入(不推荐)；<br>    d)注入类型：<br>        i.值类型(value) – 八大基本数据类型；<br>        ii.引用类型(ref) – String、自定义对象；</p>

        <h2 id="Part-2-Spring配置："   >
          <a href="#Part-2-Spring配置：" class="heading-link"><i class="fas fa-link"></i></a><a href="#Part-2-Spring配置：" class="headerlink" title="Part.2 Spring配置："></a>Part.2 Spring配置：</h2>
      <p>1、Xml配置<br>a)Bean元素：交由Spring管理的对象都要配置在bean标签中；<br>i.Bean标签介绍和创建方式：空参构造、静态工厂、动态工厂；<br>ii.Scope属性介绍：singleton、protoptype、request、session；<br>iii.初始化方法Init-method和 销毁方法destroy-method介绍；<br>b)属性注入：<br>i.Set方法注入；<br>ii.构造函数注入；<br>iii.复杂类型注入：Array、List、Set、Map、Properties</p>
<p>2、Spring注解配置；<br>a)导包和约束：基本包、aop包+context约束；<br>b)将对象注册到容器内；<br>c)用注解配置Scope属性；<br>d)注解配置init-method与destroy-method；<br>e)注解配置属性注入，值类型与引用类型； </p>
<p>3、Spring &amp; JUnit进行测试：<br>a)导包： test包（依赖 aop包）；<br>b)使用@RunWith注解创建spring容器；<br>c)使用@ContextConfiguration读取spring配置文件；<br>4、主配置文件的分包配置；</p>

        <h2 id="Part-3-小结与练习：将Spring应用到servlet项目中"   >
          <a href="#Part-3-小结与练习：将Spring应用到servlet项目中" class="heading-link"><i class="fas fa-link"></i></a><a href="#Part-3-小结与练习：将Spring应用到servlet项目中" class="headerlink" title="Part.3 小结与练习：将Spring应用到servlet项目中"></a>Part.3 小结与练习：将Spring应用到servlet项目中</h2>
      <p>1、使用servlet技术开发用户登陆功能；<br>2、在项目中加入Spring框架<br>i.将service、dao、dateSource交给Spring管理；<br>ii.在web.xml中配置Spring监听器和读取配置文件；</p>

        <h2 id="Part-4-Spring-aop："   >
          <a href="#Part-4-Spring-aop：" class="heading-link"><i class="fas fa-link"></i></a><a href="#Part-4-Spring-aop：" class="headerlink" title="Part.4 Spring-aop："></a>Part.4 Spring-aop：</h2>
      <p>1、aop思想介绍（面向切面编程）：将纵向重复代码，横向抽取解决，简称：横切<br>2、Spring中的aop：无需我们自己写动态代理的代码，spring可以将容器中管理对象生成动态代理对象，前提是我们对他进行一些设置；<br>3、Spring-aop是基于动态代理的 – 优先选用JDKProxy动态代理；<br>a)Proxy动态代理：被代理的对象必须要实现接口；<br>b)Cglib动态代理：被代理的对象不能被final修饰，基于继承；<br>4、Spring aop相关名词说明 – 详见 表-[Spring术语解释]；<br>5、Spring aop配置：<br>a)导包：<br>i.基本包；<br>ii.spring-aspects和spring-aop ；<br>iii.aop联盟包 – aopalliance；<br>iv.aop织入包 - aspectj.weaver；<br>b)自定义通知，五种自定义通知类型：<br>i.before 前置通知<br>ii.after 最终通知（后置通知）<br>iii.afterReturning 成功通知（后置通知）<br>iv.afterThrowing 异常通知（后置通知）<br>v.around 环绕通知<br>c)配置applicationContext.xml：</p>

        <h2 id="Part-5-Spring与Jdbc"   >
          <a href="#Part-5-Spring与Jdbc" class="heading-link"><i class="fas fa-link"></i></a><a href="#Part-5-Spring与Jdbc" class="headerlink" title="Part.5 Spring与Jdbc"></a>Part.5 Spring与Jdbc</h2>
      <p>1、使用JdbcTemplate操作数据库；<br>a)导包：新增jdbc、tx<br>2、JdbcTemplate的增删改查操作；<br>3、让Spring容器管理JdbcTemplate；<br>4、继承JdbcDaoSupport甩开JdbcTemplate；<br>5、在Spring中读取配置文件；</p>

        <h2 id="Part-6-Spring中的aop事务"   >
          <a href="#Part-6-Spring中的aop事务" class="heading-link"><i class="fas fa-link"></i></a><a href="#Part-6-Spring中的aop事务" class="headerlink" title="Part.6 Spring中的aop事务"></a>Part.6 Spring中的aop事务</h2>
      <p>1、事务相关知识：<br>a)什么是事务：把多条数据库操作捆绑到一起执行，要么都成功，要么都失败；<br>b)事务的原则ACID：<br>i.原子性：事务包含的所有操作，要么全部成功，要么全部失败回滚，成功全部应用到数据库，失败不能对数据库有任何影响；<br>ii.一致性：事务在执行前和执行后必须一致；例如A和B一共有100块钱，无论A、B之间如何转账，他们的钱始终相加都是100；<br>iii.隔离性：多用户并发访问同一张表时，数据库为每一个用户开启新的事务，该事务不能被其他事务所影响，相互有隔离；<br>iv.持久性：一个事务一旦提交，则对数据库中数据的改变是永久的，即便系统故障也不会丢失；<br>c)并发可能引起的问题：<br>i.脏读：一个事务读取到另一个事务未提交的数据；<br>ii.不可重复读：一个事务读取到另一个事务已提交(Update操作)的数据，导致前后读取不一致；<br>iii.幻读（虚读）：一个事务中读取到别的事务插入(Insert操作)的数据，导致前后读取不一致；<br>d)事务的隔离级别：根据实际情况选择；<br>i.Serializable串行化：可避免脏读、不可重复读和幻读；<br>ii.Repeatable read可重复读：可避免脏读、不可重复读；（MySql默认值）<br>iii.Read committed读已提交：可避免脏读；<br>iv.Read uncommitted读未提交：任何情况都无法保证；<br>2、Spring-aop事务-搭建环境；<br>a)事务基本操作：打开事务、提交事务、回滚事务；<br>b)Spring中利用接口来管理不同框架的事务操作；<br>i.通过实现PlatformTransactionManager接口支持不同的框架完成各自的事务处理；<br>ii.为不同平台提供对应的事务管理器的实现：<br>JDBC&amp;Mybatis：DataSourceTransactionManager；<br>…<br>c)Spring-aop事务通过配置事务的隔离级别、事务传播行为、是否只读来操作；<br>i.隔离级别：串行化、可重复读、读已提交、读未提交；<br>ii.是否只读：<br>1.true：不可改变数据库中的数据，查询操作推荐，<br>2.false：可以改变数据库数据；<br>iii.事务传播行为：事务方法嵌套调用的规则：<br>xService.x();    -&gt;    yService.y();<br>1.REQUIRED：如果当前没有事务，就创建一个新事务，如果当前存在事务，就加入该事务，该设置是最常用的设置；<br>2.REQUIRES_NEW：创建新事务，无论当前存不存在事务，都创建新事务；<br>3.SUPPORTS：支持当前事务，如果当前存在事务，就加入该事务，如果当前不存在事务，就以非事务执行；<br>4.NOT_SUPPORTED：以非事务方式执行操作，如果当前存在事务，就把当前事务挂起（暂停）；<br>5.MANDATORY：支持当前事务，如果当前存在事务，就加入该事务，如果当前不存在事务，就抛出异常；<br>6.NEVER：以非事务方式执行，如果当前存在事务，则抛出异常；<br>7.NESTED：如果当前存在事务，则在嵌套事务内执行。如果当前没有事务，则执行与REQUIRED类似的操作。</p>
<p>3、Spring-aop事务 – 从麻烦的事务代码中走出之xml配置版aop事务；<br>a)使用经典的转账案例进行测试，准备数据：bean、service、dao；<br>b)使用事务需要额外导入tx包和tx约束；<br>c)配置事务核心管理器: DataSourceTransactionManager；<br>d)配置事务通知 tx:Advice；<br>e)配置aop；<br>4、Spring-aop事务 – 从麻烦的事务代码中走出之注解版aop事务。</p>

        <h1 id="Part-7-Spring整合Mybatis加入事务操作数据库"   >
          <a href="#Part-7-Spring整合Mybatis加入事务操作数据库" class="heading-link"><i class="fas fa-link"></i></a><a href="#Part-7-Spring整合Mybatis加入事务操作数据库" class="headerlink" title="Part.7 Spring整合Mybatis加入事务操作数据库"></a>Part.7 Spring整合Mybatis加入事务操作数据库</h1>
      <p>1、整合Mybatis<br>a)导包：<br>i.Spring：基本包、aop、aspects、jdbc、tx、test；<br>ii.Mybatis：mybatis-3.4.6<br>iii.整合包：mybatis-spring-1.3.2<br>iv.三方包：<br>1.aopalliance<br>2.aspectj.weaver<br>3.c3p0-0.9.5.2<br>4.mchange-commons-java-0.2.11<br>5.mysql-connector-java-5.1.46-bin<br>6.ojdbc7<br>b)创建项目结构(package)：bean、service、mapper、test；<br>c)创建配置文件：sqlMapperConfig、applicaitonContext<br>2、创建测试用例：使用Mapper扫描开发，转账；<br>3、在service中加入事务：利用Spring-aop事务解决转账异常问题；</p>

        <h1 id="Spring实例一-纯Spring"   >
          <a href="#Spring实例一-纯Spring" class="heading-link"><i class="fas fa-link"></i></a><a href="#Spring实例一-纯Spring" class="headerlink" title="Spring实例一 纯Spring"></a>Spring实例一 纯Spring</h1>
      
        <h2 id="applicationContext-xml"   >
          <a href="#applicationContext-xml" class="heading-link"><i class="fas fa-link"></i></a><a href="#applicationContext-xml" class="headerlink" title="applicationContext.xml"></a>applicationContext.xml</h2>
      <figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> </span></span><br><span class="line"><span class="tag"><span class="attr">xmlns</span>=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span> </span></span><br><span class="line"><span class="tag"><span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd &quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- name 是起一个名字，我们可以通过这个name来利用容器获取对象 </span></span><br><span class="line"><span class="comment">	name 可以使用特殊字符</span></span><br><span class="line"><span class="comment">	name 可以重复 </span></span><br><span class="line"><span class="comment">	我们在实际开发中不推荐将多个对象名字命名为重复的--&gt;</span></span><br><span class="line">	</span><br><span class="line"><span class="comment">&lt;!-- id与name作用基本相同，单不推荐使用 不支持特殊字符，不能重复 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- class：是被管理对象的全包名，spring会通过这个包名来创建对象 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- request 在web环境下，如果scope属性为request 那么这个对象被创建出来 他的生命周期会与request请求一致--&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- session 同理 ，生命周期与session一致 --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;user&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.sikiedu.bean.User&quot;</span> <span class="attr">lazy-init</span>=<span class="string">&quot;default&quot;</span> <span class="attr">scope</span>=<span class="string">&quot;prototype&quot;</span> <span class="attr">init-method</span>=<span class="string">&quot;userInit&quot;</span> <span class="attr">destroy-method</span>=<span class="string">&quot;userDestroy&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;u_id&quot;</span> <span class="attr">value</span>=<span class="string">&quot;2&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 导入其他Spring的配置文件 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">import</span> <span class="attr">resource</span>=<span class="string">&quot;/applicationContext_Injection.xml&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="User-Bean对象"   >
          <a href="#User-Bean对象" class="heading-link"><i class="fas fa-link"></i></a><a href="#User-Bean对象" class="headerlink" title="User Bean对象"></a>User Bean对象</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">private</span> Integer u_id;</span><br><span class="line">	<span class="keyword">private</span> String u_username;</span><br><span class="line">	<span class="keyword">private</span> String u_password;</span><br><span class="line"></span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">(String u_username, Pet u_pet)</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;方法1 String, Pet&quot;</span>);</span><br><span class="line">		<span class="keyword">this</span>.u_username = u_username;</span><br><span class="line">		<span class="keyword">this</span>.u_pet = u_pet;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">(Integer u_username, Pet u_pet)</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;方法2 Integer, Pet&quot;</span>);</span><br><span class="line">		<span class="keyword">this</span>.u_username = u_username.toString();</span><br><span class="line">		<span class="keyword">this</span>.u_pet = u_pet;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">(Pet u_pet, Integer u_username)</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;方法3 Pet, Integer &quot;</span>);</span><br><span class="line">		<span class="keyword">this</span>.u_username = u_username.toString();</span><br><span class="line">		<span class="keyword">this</span>.u_pet = u_pet;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//加入宠物字段</span></span><br><span class="line">	<span class="keyword">private</span> Pet u_pet;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> Pet <span class="title">getU_pet</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> u_pet;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setU_pet</span><span class="params">(Pet u_pet)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.u_pet = u_pet;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;User 对象空参构造方法&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> Integer <span class="title">getU_id</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> u_id;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setU_id</span><span class="params">(Integer u_id)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.u_id = u_id;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getU_username</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> u_username;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setU_username</span><span class="params">(String u_username)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.u_username = u_username;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getU_password</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> u_password;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setU_password</span><span class="params">(String u_password)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.u_password = u_password;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">userInit</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;user init &quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">userDestroy</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;user destroy&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">&quot;User [u_id=&quot;</span> + u_id + <span class="string">&quot;, u_username=&quot;</span> + u_username + <span class="string">&quot;, u_password=&quot;</span> + u_password + <span class="string">&quot;, u_pet=&quot;</span> + u_pet</span><br><span class="line">				+ <span class="string">&quot;]&quot;</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="Test-测试方法主函数"   >
          <a href="#Test-测试方法主函数" class="heading-link"><i class="fas fa-link"></i></a><a href="#Test-测试方法主函数" class="headerlink" title="Test 测试方法主函数"></a>Test 测试方法主函数</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.sikiedu.test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.ApplicationContext;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.support.ClassPathXmlApplicationContext;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.sikiedu.bean.User;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloSpring</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//在之前项目中 new 对象是我们自己做的</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Test1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">//User u = new User();</span></span><br><span class="line">		<span class="comment">//u.setU_id(1);</span></span><br><span class="line">		<span class="comment">//System.out.println(u);</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//IOC的反转：创建对象这份工作由我们自己执行反转给spring帮我们执行；</span></span><br><span class="line">	<span class="comment">//IOC的控制：就是由spring帮我们负责创建销毁对象，掌控对象的生命周期等，我们在需要使用对象的时候跟Spring申请即可；</span></span><br><span class="line">	<span class="comment">//IOC是一种编程思想，也是一种新的设计模式，它需要DI（依赖注入）技术的支持；</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">//spring是一个容器，它将帮我们管理对象</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Test2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//根据spring配置文件获取容器对象</span></span><br><span class="line">		<span class="comment">//ApplicationContext 配置的所有bean都会在容器创建的时候被创建出来</span></span><br><span class="line">		<span class="comment">//如果配置的bean较多，那么在创建容的时候，会产生内存过大的问题；这种情况在机器硬件性能较为落后的时候体现的比较明显；</span></span><br><span class="line">		<span class="comment">//延迟加载 true就是创建容器时不加载配置的bean对象，在获取的时候才创建；</span></span><br><span class="line">		ApplicationContext ac = <span class="keyword">new</span> ClassPathXmlApplicationContext(<span class="string">&quot;applicationContext.xml&quot;</span>);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//通过getBean获取配置好的user对象（程序员向spring容器要对象）</span></span><br><span class="line">		<span class="comment">//User u = (User) ac.getBean(&quot;user&quot;);</span></span><br><span class="line">		User u = ac.getBean(User.class);</span><br><span class="line">		<span class="comment">//System.out.println(u);</span></span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>



        <h1 id="Spring实例二-Spring-Mybatis"   >
          <a href="#Spring实例二-Spring-Mybatis" class="heading-link"><i class="fas fa-link"></i></a><a href="#Spring实例二-Spring-Mybatis" class="headerlink" title="Spring实例二 Spring+Mybatis"></a>Spring实例二 Spring+Mybatis</h1>
      
        <h2 id="Bean对象-Account-java"   >
          <a href="#Bean对象-Account-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#Bean对象-Account-java" class="headerlink" title="Bean对象 Account.java"></a>Bean对象 Account.java</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Account</span> </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> Integer id;</span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	<span class="keyword">private</span> Double money;</span><br><span class="line">	</span><br><span class="line">	...忽略set，get，toString方法</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="Mapper-java文件以及Mapper-xml文件"   >
          <a href="#Mapper-java文件以及Mapper-xml文件" class="heading-link"><i class="fas fa-link"></i></a><a href="#Mapper-java文件以及Mapper-xml文件" class="headerlink" title="Mapper.java文件以及Mapper.xml文件"></a>Mapper.java文件以及Mapper.xml文件</h2>
      
        <h3 id="AcountMapper-java"   >
          <a href="#AcountMapper-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#AcountMapper-java" class="headerlink" title="AcountMapper.java"></a>AcountMapper.java</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">AccountMapper</span> </span>&#123;</span><br><span class="line">	<span class="comment">//操作数据库扣款和加款</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">//扣款</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">subMoney</span><span class="params">(Account pay)</span></span>;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//加款</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">addMoney</span><span class="params">(Account collect)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h3 id="AcountMapper-xml"   >
          <a href="#AcountMapper-xml" class="heading-link"><i class="fas fa-link"></i></a><a href="#AcountMapper-xml" class="headerlink" title="AcountMapper.xml"></a>AcountMapper.xml</h3>
      <figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">mapper</span></span></span><br><span class="line"><span class="meta">  <span class="meta-keyword">PUBLIC</span> <span class="meta-string">&quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;</span></span></span><br><span class="line"><span class="meta">  <span class="meta-string">&quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">&quot;com.sikiedu.mapper.AccountMapper&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">update</span> <span class="attr">id</span>=<span class="string">&quot;subMoney&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;Account&quot;</span>&gt;</span></span><br><span class="line">		update account set money = money - #&#123;tranferMoney&#125; where id = #&#123;id&#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">update</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="tag">&lt;<span class="name">update</span> <span class="attr">id</span>=<span class="string">&quot;addMoney&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;Account&quot;</span>&gt;</span></span><br><span class="line">		update account set money = money + #&#123;tranferMoney&#125; where id = #&#123;id&#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">update</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mapper</span>&gt;</span></span><br></pre></td></tr></table></div></figure>

        <h2 id="Service层以及实现Impl类"   >
          <a href="#Service层以及实现Impl类" class="heading-link"><i class="fas fa-link"></i></a><a href="#Service层以及实现Impl类" class="headerlink" title="Service层以及实现Impl类"></a>Service层以及实现Impl类</h2>
      
        <h3 id="AccountService-java"   >
          <a href="#AccountService-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#AccountService-java" class="headerlink" title="AccountService.java"></a>AccountService.java</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">AccountService</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//转账方法</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">updateTranferAccount</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h3 id="AccountServiceImpl-java"   >
          <a href="#AccountServiceImpl-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#AccountServiceImpl-java" class="headerlink" title="AccountServiceImpl.java"></a>AccountServiceImpl.java</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.sikiedu.service;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.annotation.Resource;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.sikiedu.bean.Account;</span><br><span class="line"><span class="keyword">import</span> com.sikiedu.mapper.AccountMapper;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AccountServiceImpl</span> <span class="keyword">implements</span> <span class="title">AccountService</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Resource(type=AccountMapper.class)</span></span><br><span class="line">	<span class="keyword">private</span> AccountMapper mapper;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateTranferAccount</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		</span><br><span class="line">		Double tranferMoney = <span class="number">1000d</span>;</span><br><span class="line">		Account pay = <span class="keyword">new</span> Account();</span><br><span class="line">		</span><br><span class="line">		pay.setId(<span class="number">1</span>);</span><br><span class="line">		pay.setTranferMoney(tranferMoney);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//先扣款</span></span><br><span class="line">		mapper.subMoney(pay);</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">int</span> a = <span class="number">1</span>/<span class="number">0</span>;</span><br><span class="line">		Account collect = <span class="keyword">new</span> Account();</span><br><span class="line">		collect.setId(<span class="number">2</span>);</span><br><span class="line">		collect.setTranferMoney(tranferMoney);</span><br><span class="line">		<span class="comment">//加款</span></span><br><span class="line">		mapper.addMoney(collect);</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="主函数调用"   >
          <a href="#主函数调用" class="heading-link"><i class="fas fa-link"></i></a><a href="#主函数调用" class="headerlink" title="主函数调用"></a>主函数调用</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RunWith(SpringJUnit4ClassRunner.class)</span></span><br><span class="line"><span class="meta">@ContextConfiguration(&quot;classpath:applicationContext.xml&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MapperTest</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Resource(name=&quot;accountService&quot;)</span></span><br><span class="line">	<span class="keyword">private</span> AccountService as;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Test1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		as.updateTranferAccount();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="Spring配置文件"   >
          <a href="#Spring配置文件" class="heading-link"><i class="fas fa-link"></i></a><a href="#Spring配置文件" class="headerlink" title="Spring配置文件"></a>Spring配置文件</h2>
      
        <h3 id="applicationContext-xml-1"   >
          <a href="#applicationContext-xml-1" class="heading-link"><i class="fas fa-link"></i></a><a href="#applicationContext-xml-1" class="headerlink" title="applicationContext.xml"></a>applicationContext.xml</h3>
      <figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:context</span>=<span class="string">&quot;http://www.springframework.org/schema/context&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:aop</span>=<span class="string">&quot;http://www.springframework.org/schema/aop&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:tx</span>=<span class="string">&quot;http://www.springframework.org/schema/tx&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd</span></span></span><br><span class="line"><span class="string"><span class="tag">		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd</span></span></span><br><span class="line"><span class="string"><span class="tag">		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd</span></span></span><br><span class="line"><span class="string"><span class="tag">		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd&quot;</span>&gt;</span></span><br><span class="line">		</span><br><span class="line">	<span class="comment">&lt;!-- 读取配置文件 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">context:property-placeholder</span> <span class="attr">location</span>=<span class="string">&quot;db.properties&quot;</span>/&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 配置 dataSource --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- db.properties文件中分别有--&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- jdbc.driverClass=com.mysql.jdbc.Driver --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_spring --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- jdbc.user=root --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- jdbc.password=123 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;dataSource&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.mchange.v2.c3p0.ComboPooledDataSource&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;driverClass&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.driverClass&#125;&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;jdbcUrl&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.jdbcUrl&#125;&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;user&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.user&#125;&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.password&#125;&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- mybatis --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;sqlSessionFactory&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.mybatis.spring.SqlSessionFactoryBean&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;dataSource&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;dataSource&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;configLocation&quot;</span> <span class="attr">value</span>=<span class="string">&quot;classpath:sqlMapConfig.xml&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- mapper工厂 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;org.mybatis.spring.mapper.MapperScannerConfigurer&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;basePackage&quot;</span> <span class="attr">value</span>=<span class="string">&quot;com.sikiedu.mapper&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- service --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;accountService&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.sikiedu.service.AccountServiceImpl&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 需要事务核心管理器 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;transactionManager&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;dataSource&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;dataSource&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 配置事务通知 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">tx:advice</span> <span class="attr">id</span>=<span class="string">&quot;txAdvice&quot;</span> <span class="attr">transaction-manager</span>=<span class="string">&quot;transactionManager&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">tx:attributes</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;update*&quot;</span> <span class="attr">isolation</span>=<span class="string">&quot;DEFAULT&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;REQUIRED&quot;</span> <span class="attr">read-only</span>=<span class="string">&quot;false&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">tx:attributes</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">tx:advice</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 配置aop --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">expression</span>=<span class="string">&quot;execution(* com.sikiedu.service.*ServiceImpl.*(..))&quot;</span> <span class="attr">id</span>=<span class="string">&quot;txPc&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:advisor</span> <span class="attr">advice-ref</span>=<span class="string">&quot;txAdvice&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;txPc&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br><span class="line">	</span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span>mapper工厂 --&gt;</span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;org.mybatis.spring.mapper.MapperScannerConfigurer&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;basePackage&quot;</span> <span class="attr">value</span>=<span class="string">&quot;com.sikiedu.mapper&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- service --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;accountService&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.sikiedu.service.AccountServiceImpl&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 需要事务核心管理器 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;transactionManager&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;dataSource&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;dataSource&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 配置事务通知 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">tx:advice</span> <span class="attr">id</span>=<span class="string">&quot;txAdvice&quot;</span> <span class="attr">transaction-manager</span>=<span class="string">&quot;transactionManager&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">tx:attributes</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;update*&quot;</span> <span class="attr">isolation</span>=<span class="string">&quot;DEFAULT&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;REQUIRED&quot;</span> <span class="attr">read-only</span>=<span class="string">&quot;false&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">tx:attributes</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">tx:advice</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 配置aop --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">expression</span>=<span class="string">&quot;execution(* com.sikiedu.service.*ServiceImpl.*(..))&quot;</span> <span class="attr">id</span>=<span class="string">&quot;txPc&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:advisor</span> <span class="attr">advice-ref</span>=<span class="string">&quot;txAdvice&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;txPc&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br><span class="line">	</span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h2 id="Mybatis配置文件"   >
          <a href="#Mybatis配置文件" class="heading-link"><i class="fas fa-link"></i></a><a href="#Mybatis配置文件" class="headerlink" title="Mybatis配置文件"></a>Mybatis配置文件</h2>
      
        <h3 id="sqlMapConfig-xml"   >
          <a href="#sqlMapConfig-xml" class="heading-link"><i class="fas fa-link"></i></a><a href="#sqlMapConfig-xml" class="headerlink" title="sqlMapConfig.xml"></a>sqlMapConfig.xml</h3>
      <figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">configuration</span></span></span><br><span class="line"><span class="meta">  <span class="meta-keyword">PUBLIC</span> <span class="meta-string">&quot;-//mybatis.org//DTD Config 3.0//EN&quot;</span></span></span><br><span class="line"><span class="meta">  <span class="meta-string">&quot;http://mybatis.org/dtd/mybatis-3-config.dtd&quot;</span>&gt;</span></span><br><span class="line">  </span><br><span class="line">  <span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">  	<span class="tag">&lt;<span class="name">typeAliases</span>&gt;</span></span><br><span class="line">  		<span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">&quot;com.sikiedu.bean&quot;</span>/&gt;</span></span><br><span class="line">  	<span class="tag">&lt;/<span class="name">typeAliases</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></div></figure>


        <h1 id="Spring实例三-Spring-AOP"   >
          <a href="#Spring实例三-Spring-AOP" class="heading-link"><i class="fas fa-link"></i></a><a href="#Spring实例三-Spring-AOP" class="headerlink" title="Spring实例三 Spring+AOP"></a>Spring实例三 Spring+AOP</h1>
      
        <h2 id="自定义通知类-MyAdvice-java"   >
          <a href="#自定义通知类-MyAdvice-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#自定义通知类-MyAdvice-java" class="headerlink" title="自定义通知类 MyAdvice.java"></a>自定义通知类 MyAdvice.java</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyAdvice</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//before 前置通知 在目标方法前调用</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">before</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;before&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//after 最终通知（后置通知）在目标方法后调用，无论是否出现异常都会执行 finally</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">after</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;after&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//afterReturning 成功通知（后置通知）在目标方法执行后，并且执行成功，如果方法出现异常则不调用</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">afterReturning</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;afterReturning&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//afterThrowing 异常通知（后置通知）在目标方法执行出现异常的时候才会调用</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">afterThrowing</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;afterThrowing&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//around 环绕通知 需要我们手动调用目标方法，并且可以设置通知</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Object <span class="title">around</span><span class="params">(ProceedingJoinPoint pjp)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;around before&quot;</span>);</span><br><span class="line">		Object proceed = pjp.proceed();</span><br><span class="line">		System.out.println(<span class="string">&quot;around after&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> proceed;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="Service以及实现类impl"   >
          <a href="#Service以及实现类impl" class="heading-link"><i class="fas fa-link"></i></a><a href="#Service以及实现类impl" class="headerlink" title="Service以及实现类impl"></a>Service以及实现类impl</h2>
      
        <h3 id="UserService-java"   >
          <a href="#UserService-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#UserService-java" class="headerlink" title="UserService.java"></a>UserService.java</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line">	<span class="comment">//增</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">save</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="comment">//删</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">delete</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="comment">//改</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">update</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="comment">//查</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">find</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="UserServiceImpl-java"   >
          <a href="#UserServiceImpl-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#UserServiceImpl-java" class="headerlink" title="UserServiceImpl.java"></a>UserServiceImpl.java</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserServiceImpl</span> <span class="keyword">implements</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">save</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;save&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">delete</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;delete&quot;</span>);</span><br><span class="line">		<span class="comment">//int a = 1/0;</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">update</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;update&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">find</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;find&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="Spring配置文件-applicationContext-xml"   >
          <a href="#Spring配置文件-applicationContext-xml" class="heading-link"><i class="fas fa-link"></i></a><a href="#Spring配置文件-applicationContext-xml" class="headerlink" title="Spring配置文件 applicationContext.xml"></a>Spring配置文件 applicationContext.xml</h2>
      <figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:aop</span>=<span class="string">&quot;http://www.springframework.org/schema/aop&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd</span></span></span><br><span class="line"><span class="string"><span class="tag">		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd&quot;</span>&gt;</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">&lt;!-- 目标对象 --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;userService&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.sikiedu.service.UserServiceImpl&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">&lt;!-- 通知对象 --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;myAdvice&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.sikiedu.aop.MyAdvice&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">		</span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">			<span class="comment">&lt;!-- 切入点  expression 切入点表达式 可以配置要增强的方法</span></span><br><span class="line"><span class="comment">								public void com.sikiedu.service.UserServiceImpl.save()</span></span><br><span class="line"><span class="comment">								* com.sikiedu.service.*ServiceImpl.*(..)</span></span><br><span class="line"><span class="comment">							   id 就是唯一标识</span></span><br><span class="line"><span class="comment">			--&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">expression</span>=<span class="string">&quot;execution(* com.sikiedu.service.*ServiceImplabc.*(..))&quot;</span> <span class="attr">id</span>=<span class="string">&quot;servicePc&quot;</span>/&gt;</span></span><br><span class="line">			</span><br><span class="line">			<span class="comment">&lt;!-- 切面 通知+切入点 --&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">&quot;myAdvice&quot;</span>&gt;</span></span><br><span class="line">				<span class="comment">&lt;!-- 通知类型 --&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">aop:before</span> <span class="attr">method</span>=<span class="string">&quot;before&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;servicePc&quot;</span>/&gt;</span></span><br><span class="line">				<span class="comment">&lt;!-- 最终通知 后置通知 --&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">aop:after</span> <span class="attr">method</span>=<span class="string">&quot;after&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;servicePc&quot;</span>/&gt;</span></span><br><span class="line">				<span class="comment">&lt;!-- 成功通知 后置通知 --&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">aop:after-returning</span> <span class="attr">method</span>=<span class="string">&quot;afterReturning&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;servicePc&quot;</span>/&gt;</span></span><br><span class="line">				<span class="comment">&lt;!-- 异常通知 后置通知 --&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">aop:after-throwing</span> <span class="attr">method</span>=<span class="string">&quot;afterThrowing&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;servicePc&quot;</span>/&gt;</span></span><br><span class="line">				<span class="comment">&lt;!-- 环绕通知 --&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">aop:around</span> <span class="attr">method</span>=<span class="string">&quot;around&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;servicePc&quot;</span>/&gt;</span></span><br><span class="line">			<span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br><span class="line">		</span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="主函数测试方法"   >
          <a href="#主函数测试方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#主函数测试方法" class="headerlink" title="主函数测试方法"></a>主函数测试方法</h2>
      
        <h3 id="AopTest-java"   >
          <a href="#AopTest-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#AopTest-java" class="headerlink" title="AopTest.java"></a>AopTest.java</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RunWith(SpringJUnit4ClassRunner.class)</span></span><br><span class="line"><span class="meta">@ContextConfiguration(&quot;classpath:applicationContext.xml&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AopTest</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Resource(name=&quot;userService&quot;)</span></span><br><span class="line">	UserService us;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Test1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		UserServiceProxy usProxy = <span class="keyword">new</span> UserServiceProxy();</span><br><span class="line">		UserService us = <span class="keyword">new</span> UserServiceImpl();</span><br><span class="line">		UserService us_PowerUp = usProxy.getUserServiceProxy(us);</span><br><span class="line">		us_PowerUp.find();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Test2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		us.delete();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="UserServiceProxy-java"   >
          <a href="#UserServiceProxy-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#UserServiceProxy-java" class="headerlink" title="UserServiceProxy.java"></a>UserServiceProxy.java</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.sikiedu.test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.InvocationHandler;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Proxy;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.sikiedu.service.UserService;</span><br><span class="line"><span class="keyword">import</span> com.sikiedu.service.UserServiceImpl;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * UserService代理类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Joey</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserServiceProxy</span> </span>&#123;</span><br><span class="line">	<span class="comment">//UserServiceProxy</span></span><br><span class="line">	<span class="comment">//UserService</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> UserService <span class="title">getUserServiceProxy</span><span class="params">(UserService us)</span> </span>&#123;</span><br><span class="line">		</span><br><span class="line">	<span class="keyword">return</span> (UserService) Proxy.newProxyInstance(UserServiceProxy.class.getClassLoader(), </span><br><span class="line">		UserServiceImpl.class.getInterfaces(), </span><br><span class="line">		<span class="keyword">new</span> InvocationHandler() &#123;</span><br><span class="line">		<span class="meta">@Override</span></span><br><span class="line">		<span class="function"><span class="keyword">public</span> Object <span class="title">invoke</span><span class="params">(Object proxy, </span></span></span><br><span class="line"><span class="params"><span class="function">				Method method, </span></span></span><br><span class="line"><span class="params"><span class="function">				Object[] args)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">			<span class="comment">//增强代码</span></span><br><span class="line">			System.out.println(<span class="string">&quot;开启事务&quot;</span>);</span><br><span class="line">			<span class="comment">//调用原始方法</span></span><br><span class="line">			Object invoke = method.invoke(us, args);</span><br><span class="line">			System.out.println(<span class="string">&quot;提交/回滚&quot;</span>);</span><br><span class="line">			<span class="keyword">return</span> invoke;</span><br><span class="line">		&#125;</span><br><span class="line">		&#125;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></div></figure></div><footer class="post-footer"><div class="post-ending ending"><div class="ending__text">------ 本文结束，感谢您的阅读 ------</div></div><div class="post-copyright copyright"><div class="copyright-author"><span class="copyright-author__name">本文作者: </span><span class="copyright-author__value"><a href="https://bruce-jj.gitee.io/blog">何水浪</a></span></div><div class="copyright-link"><span class="copyright-link__name">本文链接: </span><span class="copyright-link__value"><a href="https://bruce-jj.gitee.io/blog/2021/03/16/Spring/">https://bruce-jj.gitee.io/blog/2021/03/16/Spring/</a></span></div><div class="copyright-notice"><span class="copyright-notice__name">版权声明: </span><span class="copyright-notice__value">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" rel="external nofollow" target="_blank">BY-NC-SA</a> 许可协议。转载请注明出处！</span></div></div><div class="post-tags"><span class="post-tags-item"><span class="post-tags-item__icon"><i class="fas fa-tag"></i></span><a class="post-tags-item__link" href="https://bruce-jj.gitee.io/blog/tags/SSM%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">SSM学习笔记</a></span></div><nav class="post-paginator paginator"><div class="paginator-prev"><a class="paginator-prev__link" href="/blog/2021/03/16/%E7%BF%BB%E8%AF%91%E4%BD%9C%E5%93%81-%E5%A4%A7%E6%95%B0%E6%8D%AE/"><span class="paginator-prev__icon"><i class="fas fa-angle-left"></i></span><span class="paginator-prev__text">翻译作品-大数据</span></a></div><div class="paginator-next"><a class="paginator-next__link" href="/blog/2021/03/15/JavaScript/"><span class="paginator-prev__text">JavaScript学习笔记</span><span class="paginator-next__icon"><i class="fas fa-angle-right"></i></span></a></div></nav></footer></div></div><div class="comments" id="comments"><div id="valine-container"></div></div></div><div class="sidebar-wrap" id="sidebar-wrap"><aside class="sidebar" id="sidebar"><div class="sidebar-nav"><span class="sidebar-nav-toc current">文章目录</span><span class="sidebar-nav-ov">站点概览</span></div><section class="sidebar-toc"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#SSM%E6%A1%86%E6%9E%B6%E7%B3%BB%E5%88%97%E4%B9%8B%E4%BA%8C-%E5%A4%A7%E7%AE%A1%E5%AE%B6Spring"><span class="toc-number">1.</span> <span class="toc-text">
          SSM框架系列之二 大管家Spring</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Spring%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="toc-number">1.1.</span> <span class="toc-text">
          Spring是什么？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E8%83%BD%E4%BB%8E%E8%BF%99%E9%97%A8%E8%AF%BE%E4%B8%AD%E5%AD%A6%E5%88%B0%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="toc-number">1.2.</span> <span class="toc-text">
          我能从这门课中学到什么？</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%BC%80%E5%A7%8B%E5%AD%A6%E4%B9%A0Spring%E6%A1%86%E6%9E%B6"><span class="toc-number">2.</span> <span class="toc-text">
          开始学习Spring框架</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Part-1-Spring%E6%A1%86%E6%9E%B6%E4%BB%8B%E7%BB%8D%E4%B8%8E%E5%85%A5%E9%97%A8%E6%A1%88%E4%BE%8B%EF%BC%9A"><span class="toc-number">2.1.</span> <span class="toc-text">
          Part.1 Spring框架介绍与入门案例：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Part-2-Spring%E9%85%8D%E7%BD%AE%EF%BC%9A"><span class="toc-number">2.2.</span> <span class="toc-text">
          Part.2 Spring配置：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Part-3-%E5%B0%8F%E7%BB%93%E4%B8%8E%E7%BB%83%E4%B9%A0%EF%BC%9A%E5%B0%86Spring%E5%BA%94%E7%94%A8%E5%88%B0servlet%E9%A1%B9%E7%9B%AE%E4%B8%AD"><span class="toc-number">2.3.</span> <span class="toc-text">
          Part.3 小结与练习：将Spring应用到servlet项目中</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Part-4-Spring-aop%EF%BC%9A"><span class="toc-number">2.4.</span> <span class="toc-text">
          Part.4 Spring-aop：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Part-5-Spring%E4%B8%8EJdbc"><span class="toc-number">2.5.</span> <span class="toc-text">
          Part.5 Spring与Jdbc</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Part-6-Spring%E4%B8%AD%E7%9A%84aop%E4%BA%8B%E5%8A%A1"><span class="toc-number">2.6.</span> <span class="toc-text">
          Part.6 Spring中的aop事务</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Part-7-Spring%E6%95%B4%E5%90%88Mybatis%E5%8A%A0%E5%85%A5%E4%BA%8B%E5%8A%A1%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-number">3.</span> <span class="toc-text">
          Part.7 Spring整合Mybatis加入事务操作数据库</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Spring%E5%AE%9E%E4%BE%8B%E4%B8%80-%E7%BA%AFSpring"><span class="toc-number">4.</span> <span class="toc-text">
          Spring实例一 纯Spring</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#applicationContext-xml"><span class="toc-number">4.1.</span> <span class="toc-text">
          applicationContext.xml</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#User-Bean%E5%AF%B9%E8%B1%A1"><span class="toc-number">4.2.</span> <span class="toc-text">
          User Bean对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Test-%E6%B5%8B%E8%AF%95%E6%96%B9%E6%B3%95%E4%B8%BB%E5%87%BD%E6%95%B0"><span class="toc-number">4.3.</span> <span class="toc-text">
          Test 测试方法主函数</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Spring%E5%AE%9E%E4%BE%8B%E4%BA%8C-Spring-Mybatis"><span class="toc-number">5.</span> <span class="toc-text">
          Spring实例二 Spring+Mybatis</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Bean%E5%AF%B9%E8%B1%A1-Account-java"><span class="toc-number">5.1.</span> <span class="toc-text">
          Bean对象 Account.java</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Mapper-java%E6%96%87%E4%BB%B6%E4%BB%A5%E5%8F%8AMapper-xml%E6%96%87%E4%BB%B6"><span class="toc-number">5.2.</span> <span class="toc-text">
          Mapper.java文件以及Mapper.xml文件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#AcountMapper-java"><span class="toc-number">5.2.1.</span> <span class="toc-text">
          AcountMapper.java</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#AcountMapper-xml"><span class="toc-number">5.2.2.</span> <span class="toc-text">
          AcountMapper.xml</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Service%E5%B1%82%E4%BB%A5%E5%8F%8A%E5%AE%9E%E7%8E%B0Impl%E7%B1%BB"><span class="toc-number">5.3.</span> <span class="toc-text">
          Service层以及实现Impl类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#AccountService-java"><span class="toc-number">5.3.1.</span> <span class="toc-text">
          AccountService.java</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#AccountServiceImpl-java"><span class="toc-number">5.3.2.</span> <span class="toc-text">
          AccountServiceImpl.java</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%BB%E5%87%BD%E6%95%B0%E8%B0%83%E7%94%A8"><span class="toc-number">5.4.</span> <span class="toc-text">
          主函数调用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Spring%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">5.5.</span> <span class="toc-text">
          Spring配置文件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#applicationContext-xml-1"><span class="toc-number">5.5.1.</span> <span class="toc-text">
          applicationContext.xml</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Mybatis%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">5.6.</span> <span class="toc-text">
          Mybatis配置文件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#sqlMapConfig-xml"><span class="toc-number">5.6.1.</span> <span class="toc-text">
          sqlMapConfig.xml</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Spring%E5%AE%9E%E4%BE%8B%E4%B8%89-Spring-AOP"><span class="toc-number">6.</span> <span class="toc-text">
          Spring实例三 Spring+AOP</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E9%80%9A%E7%9F%A5%E7%B1%BB-MyAdvice-java"><span class="toc-number">6.1.</span> <span class="toc-text">
          自定义通知类 MyAdvice.java</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Service%E4%BB%A5%E5%8F%8A%E5%AE%9E%E7%8E%B0%E7%B1%BBimpl"><span class="toc-number">6.2.</span> <span class="toc-text">
          Service以及实现类impl</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#UserService-java"><span class="toc-number">6.2.1.</span> <span class="toc-text">
          UserService.java</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#UserServiceImpl-java"><span class="toc-number">6.2.2.</span> <span class="toc-text">
          UserServiceImpl.java</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Spring%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6-applicationContext-xml"><span class="toc-number">6.3.</span> <span class="toc-text">
          Spring配置文件 applicationContext.xml</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%BB%E5%87%BD%E6%95%B0%E6%B5%8B%E8%AF%95%E6%96%B9%E6%B3%95"><span class="toc-number">6.4.</span> <span class="toc-text">
          主函数测试方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#AopTest-java"><span class="toc-number">6.4.1.</span> <span class="toc-text">
          AopTest.java</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#UserServiceProxy-java"><span class="toc-number">6.4.2.</span> <span class="toc-text">
          UserServiceProxy.java</span></a></li></ol></li></ol></li></ol></section><!-- ov = overview--><section class="sidebar-ov hide"><div class="sidebar-ov-author"><div class="sidebar-ov-author__avatar"><img class="sidebar-ov-author__avatar_img" src="/blog/images/avatar.jpg" alt="avatar"></div><p class="sidebar-ov-author__text">C'est la vie</p></div><div class="sidebar-ov-state"><a class="sidebar-ov-state-item sidebar-ov-state-item--posts" href="/blog/archives/"><div class="sidebar-ov-state-item__count">48</div><div class="sidebar-ov-state-item__name">归档</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--categories" href="/blog/categories/"><div class="sidebar-ov-state-item__count">19</div><div class="sidebar-ov-state-item__name">分类</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--tags" href="/blog/tags/"><div class="sidebar-ov-state-item__count">20</div><div class="sidebar-ov-state-item__name">标签</div></a></div><div class="sidebar-ov-cc"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" target="_blank" rel="noopener" data-popover="知识共享许可协议" data-popover-pos="up"><img src="/blog/images/cc-by-nc-sa.svg"></a></div></section><div class="sidebar-reading"><div class="sidebar-reading-info"><span class="sidebar-reading-info__text">你已阅读了 </span><span class="sidebar-reading-info__num">0</span><span class="sidebar-reading-info__perc">%</span></div><div class="sidebar-reading-line"></div></div></aside></div><div class="clearfix"></div></div></main><footer class="footer" id="footer"><div class="footer-inner"><div><span>Copyright © 2021</span><span class="footer__icon"><i class="fas fa-heart"></i></span><span>何水浪</span></div><div><span>由 <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a> 强力驱动</span><span> v5.4.0</span><span class="footer__devider">|</span><span>主题 - <a href="https://github.com/liuyib/hexo-theme-stun/" title="Stun" target="_blank" rel="noopener">Stun</a></span><span> v2.6.2</span></div></div></footer><div class="loading-bar" id="loading-bar"><div class="loading-bar__progress"></div></div><div class="back2top" id="back2top"><span class="back2top__icon"><i class="fas fa-rocket"></i></span></div></div><div class="search-mask"></div><div class="search-popup"><span class="search-close"></span><div class="search-input"><input placeholder="搜索文章（支持多关键词，请用空格分隔）"></div><div class="search-results"></div></div><script src="https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js"></script><script>function initSearch() {
  var isXML = true;
  var search_path = 'search.json';

  if (!search_path) {
    search_path = 'search.xml';
  } else if (/json$/i.test(search_path)) {
    isXML = false;
  }

  var path = '/blog/' + search_path;
  $.ajax({
    url: path,
    dataType: isXML ? 'xml' : 'json',
    async: true,
    success: function (res) {
      var datas = isXML ? $('entry', res).map(function () {
        // 将 XML 转为 JSON
        return {
          title: $('title', this).text(),
          content: $('content', this).text(),
          url: $('url', this).text()
        };
      }).get() : res;
      var $input = $('.search-input input');
      var $result = $('.search-results');
      // 搜索对象（标题、内容）的权重，影响显示顺序
      var WEIGHT = { title: 100, content: 1 };
      var searchPost = function () {
        var searchText = $input.val().toLowerCase().trim();
        // 根据空白字符分隔关键字
        var keywords = searchText.split(/[\s]+/);
        // 搜索结果
        var matchPosts = [];

        // 有多个关键字时，将原文字整个保存下来
        if (keywords.length > 1) {
          keywords.push(searchText);
        }
        // 防止未输入字符时搜索
        if (searchText.length > 0) {
          datas.forEach(function (data) {
            var isMatch  = false;
            // 没有标题的文章使用预设的 i18n 变量代替
            var title = (data.title && data.title.trim()) || '[ 文章无标题 ]';
            var titleLower = title && title.toLowerCase();
            // 删除 HTML 标签 和 所有空白字符
            var content = data.content && data.content.replace(/<[^>]+>/g, '');
            var contentLower = content && content.toLowerCase();
            // 删除重复的 /
            var postURL = data.url && decodeURI(data.url).replace(/\/{2,}/g, '/');
            // 标题中匹配到的关键词
            var titleHitSlice = [];
            // 内容中匹配到的关键词
            var contentHitSlice = [];

            keywords.forEach(function (keyword) {
              /**
              * 获取匹配的关键词的索引
              * @param {String} keyword 要匹配的关键字
              * @param {String} text 原文字
              * @param {Boolean} caseSensitive 是否区分大小写
              * @param {Number} weight 匹配对象的权重。权重大的优先显示
              * @return {Array}
              */
              function getIndexByword (word, text, caseSensitive, weight) {
                if (!word || !text) {
                  return [];
                };

                var startIndex = 0; // 每次匹配的开始索引
                var index = -1;     // 匹配到的索引值
                var result = [];    // 匹配结果

                if (!caseSensitive) {
                  word = word.toLowerCase();
                  text = text.toLowerCase();
                }

                while((index = text.indexOf(word, startIndex)) !== -1) {
                  var hasMatch = false;
                  // 索引位置相同的关键词，保留长度较长的
                  titleHitSlice.forEach(function (hit) {
                    if (hit.index === index && hit.word.length < word.length) {
                      hit.word = word;
                      hasMatch = true;
                    }
                  });
                  startIndex = index + word.length;
                  !hasMatch && result.push({ index: index, word: word, weight: weight });
                }
                return result;
              }
              titleHitSlice = titleHitSlice.concat(getIndexByword(keyword, titleLower, false, WEIGHT.title));
              contentHitSlice = contentHitSlice.concat(getIndexByword(keyword, contentLower, false, WEIGHT.content));
            });

            var hitTitle = titleHitSlice.length;
            var hitContent = contentHitSlice.length;

            if (hitTitle > 0 || hitContent > 0) {
              isMatch = true;
            }
            if (isMatch) {
              ;[titleHitSlice, contentHitSlice].forEach(function (hit) {
                // 按照匹配文字的索引的递增顺序排序
                hit.sort(function (left, right) {
                  return left.index - right.index;
                });
              });
              /**
              * 给文本中匹配到的关键词添加标记，从而进行高亮显示
              * @param {String} text 原文本
              * @param {Array} hitSlice 匹配项的索引信息
              * @param {Number} start 开始索引
              * @param {Number} end 结束索引
              * @return {String}
              */
              function highlightKeyword (text, hitSlice, start, end) {
                if (!text || !hitSlice || !hitSlice.length) {
                  return;
                }

                var result = '';
                var startIndex = start;
                var endIndex = end;
                hitSlice.forEach(function (hit) {
                  if (hit.index < startIndex) {
                    return;
                  }

                  var hitWordEnd = hit.index + hit.word.length;
                  result += text.slice(startIndex, hit.index);
                  result += '<b>' + text.slice(hit.index, hitWordEnd) + '</b>';
                  startIndex = hitWordEnd;
                });
                result += text.slice(startIndex, endIndex);
                return result;
              }

              var postData = {};
              // 文章总的搜索权重
              var postWeight = titleHitSlice.length * WEIGHT.title + contentHitSlice.length * WEIGHT.content;
              // 标记匹配关键词后的标题
              var postTitle = highlightKeyword(title, titleHitSlice, 0, title.length) || title;
              // 标记匹配关键词后的内容
              var postContent;
              // 显示内容的长度
              var SHOW_WORD_LENGTH = 200;
              // 命中关键词前的字符显示长度
              var SHOW_WORD_FRONT_LENGTH = 20;
              var SHOW_WORD_END_LENGTH = SHOW_WORD_LENGTH - SHOW_WORD_FRONT_LENGTH;

              // 截取匹配的第一个字符，前后共 200 个字符来显示
              if (contentHitSlice.length > 0) {
                var firstIndex = contentHitSlice[0].index;
                var start = firstIndex > SHOW_WORD_FRONT_LENGTH ? firstIndex - SHOW_WORD_FRONT_LENGTH : 0;
                var end = firstIndex + SHOW_WORD_END_LENGTH;
                postContent = highlightKeyword(content, contentHitSlice, start, end);
              } else { // 未匹配到内容，直接截取前 200 个字符来显示
                postContent = content.slice(0, SHOW_WORD_LENGTH);
              }
              postData.title = postTitle;
              postData.content = postContent;
              postData.url = postURL;
              postData.weight = postWeight;
              matchPosts.push(postData);
            }
          });
        }

        var resultInnerHtml = '';
        if (matchPosts.length) {
          // 按权重递增的顺序排序，使权重大的优先显示
          matchPosts.sort(function (left, right) {
            return right.weight - left.weight;
          });
          resultInnerHtml += '<ul>';
          matchPosts.forEach(function (post) {
            resultInnerHtml += '<li><a class="search-results-title" href="' + post.url + '">';
            resultInnerHtml += post.title;
            resultInnerHtml += '</a><div class="search-results-content">';
            resultInnerHtml += post.content;
            resultInnerHtml += '</div></li>';
          });
          resultInnerHtml += '</ul>';
        } else {
          resultInnerHtml += '<div class="search-results-none"><i class="far fa-meh"></i></div>';
        }
        $result.html(resultInnerHtml);
      };
      $input.on('input', searchPost);
      $input.on('keyup', function (e) {
        if (e.keyCode === Stun.utils.codeToKeyCode('Enter')) {
          searchPost();
        }
      });
    }
  });
}

function closeSearch () {
  $('body').css({ overflow: 'auto' });
  $('.search-popup').css({ display: 'none' });
  $('.search-mask').css({ display: 'none' });
}

window.addEventListener('DOMContentLoaded', function () {
  Stun.utils.pjaxReloadLocalSearch = function () {
    $('.header-nav-search').on('click', function (e) {
      e.stopPropagation();
      $('body').css('overflow', 'hidden');
      $('.search-popup')
        .velocity('stop')
        .velocity('transition.expandIn', {
          duration: 300,
          complete: function () {
            $('.search-popup input').focus();
          }
        });
      $('.search-mask')
        .velocity('stop')
        .velocity('transition.fadeIn', {
          duration: 300
        });

      initSearch();
    });
    $('.search-mask, .search-close').on('click', function () {
      closeSearch();
    });
    $(document).on('keydown', function (e) {
      // Escape <=> 27
      if (e.keyCode === Stun.utils.codeToKeyCode('Escape')) {
        closeSearch();
      }
    });
  };

  Stun.utils.pjaxReloadLocalSearch();
}, false);

function safeOpenUrl(url) {
  var newTab = window.open();
  newTab.opener = null;
  newTab.location = url;
}

function extSearch(engine) {
  var engines = {
    google: 'https://www.google.com/search?q=',
    bing: 'https://cn.bing.com/search?q=',
    baidu: 'https://www.baidu.com/s?ie=UTF-8&wd=',
  };
  var host = window.location.host;
  var query = $('.search-input input').val().toLowerCase().trim();
  var uri = engines[engine] + query + ' site:' + host;

  if (query) {
    safeOpenUrl(uri);
  } else {
    Stun.utils.popAlert('warning', '请输入字符');
  }
}

var assistSearchList = window.CONFIG.assistSearch;

if (Array.isArray(assistSearchList)) {
  assistSearchList.forEach(function (name) {
    document.querySelector('.search-btns-item--' + name).addEventListener('click', function () {
      extSearch(name);
    }, false);
  });
}</script><script src="https://cdn.jsdelivr.net/npm/leancloud-storage@latest/dist/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine@latest/dist/Valine.min.js"></script><script>function loadValine () {
  var GUEST_INFO = ['nick', 'mail', 'link'];
  var guest_info = 'nick';

  guest_info = guest_info.split(',').filter(function(item) {
    return GUEST_INFO.indexOf(item) > -1;
  });
  new Valine({
    el: '#valine-container',
    appId: 'fJ4fFnieffDVKy4Jkn16aHdO-gzGzoHsz',
    appKey: 'qfVo2CUC2pJ3udFzLFnd0GBM',
    notify: true,
    verify: true,
    placeholder: '说点啥再走呗~爱你',
    avatar: 'mp',
    meta: guest_info,
    pageSize: '10' || 10,
    visitor: false,
    recordIP: false,
    lang: '' || 'zh-cn',
    path: window.location.pathname
  });
}

if (false) {
  loadValine();
} else {
  window.addEventListener('DOMContentLoaded', loadValine, false);
}</script><script src="/blog/js/utils.js?v=2.6.2"></script><script src="/blog/js/stun-boot.js?v=2.6.2"></script><script src="/blog/js/scroll.js?v=2.6.2"></script><script src="/blog/js/header.js?v=2.6.2"></script><script src="/blog/js/sidebar.js?v=2.6.2"></script><script type="application/json" src="/blog/search.json"></script></body></html>